home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Open Transport / OT1.1 Developer Release / Open Transport SDK / Open Tpt Client Developer / Samples / Internet / OTTcpPitchSample.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  10.5 KB  |  468 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OTTcpPitchSample.cp
  3.  
  4.     Contains:    Tcp pitch sample.
  5.  
  6.     Copyright:    © 1993-1995 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. // OT TCP Pitch Test Program (as an SIOW app)
  12.  
  13. #include <QuickDraw.h>
  14. #include <stdio.h>
  15. #include <StdLib.h>
  16. #include <TextUtils.h>
  17. #include <strings.h>
  18. #include <String.h>
  19. #include <Events.h>
  20. #include <Desk.h>
  21. #include <OpenTransport.h>
  22. #include <OpenTptInternet.h>
  23.  
  24. #ifndef T_DISCON_IND
  25. #define T_DISCON_IND 128
  26. #endif
  27.  
  28.  
  29. /*******************************************************************************
  30. ** GLOBAL VARIABLES
  31. ********************************************************************************/
  32.  
  33. #define kMaxDataLen 256
  34.  
  35. const size_t    kMyPoolSize = 60000;
  36.  
  37. InetPort    gCatchPort        = 0;
  38. InetHost    gCatchIpAddr    = 0;
  39. InetPort      gPitchPort        = 0;
  40. InetHost    gPitchIpAddr    = 0;
  41.  
  42. unsigned short gBindCompleted        = 0;
  43. unsigned short gCallRcvOrdDiscon    = 0;
  44.  
  45. struct InetAddress sndsin, rcvsin, reqsin, retsin;
  46. char data[kMaxDataLen];
  47. char defaultData[] = "Go Cal, beat Stanford !!!";
  48.  
  49. /*******************************************************************************
  50. ** Function Prototypes
  51. ********************************************************************************/
  52.  
  53. void Inits();
  54. void CleanUp();
  55. void Idle();    
  56. void DoIt();
  57.  
  58.  
  59. /*******************************************************************************
  60. **  main function
  61. ********************************************************************************/
  62.  
  63. void main()
  64. {
  65.     Inits();
  66.     DoIt();
  67.     CleanUp();
  68. }
  69.  
  70. /*******************************************************************************
  71. ** Initialize Quickdraw and ASLM
  72. ********************************************************************************/
  73.  
  74. void Inits()
  75. {
  76.     InitGraf(&qd.thePort);
  77.     if (InitOpenTransport() != kOTNoError)
  78.     {
  79.         fprintf(stderr, "OTTcpPitch: Could not initialize ASLM, exiting\n");
  80.         exit(1);
  81.     }
  82. }
  83.  
  84. /*******************************************************************************
  85. ** ShowEndpointOptions
  86. ********************************************************************************/
  87.  
  88. void ShowEndpointOptions(TEndpoint*    ep)
  89. {
  90.     /*
  91.      *    This function retrieves and displays 
  92.      *    the IP and TCPP endpoint options for this endpoint.
  93.      */
  94.      
  95.     OSStatus    err;
  96.     TOptMgmt*            ret = (TOptMgmt*)OTAlloc(ep, T_OPTMGMT, T_OPT, &err);
  97.  
  98.     
  99.     do
  100.     {
  101.         fprintf(stderr, "Current Readable IP Option Settings for endpoint @ %08lX:\n", ep);
  102.         if ( ret == NULL )
  103.         {
  104.             fprintf(stderr, "ERROR: could not allocate TOptMgmt structure (%d)\n", err);
  105.             break;
  106.         }
  107.         //
  108.         // Get the current IP options
  109.         //
  110.         TOptMgmt        req;
  111.         TOptionHeader    option;
  112.         
  113.         option.len        = kOTOptionHeaderSize;
  114.         option.level    = INET_IP;
  115.         option.name        = T_ALLOPT;
  116.         
  117.         req.opt.buf = (UInt8*)&option;
  118.         req.opt.len    = kOTOptionHeaderSize;
  119.         req.flags    = T_CURRENT;
  120.         
  121.         err = ep->OptionManagement(&req, ret);
  122.         if ( err != kOTNoError )
  123.         {
  124.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  125.             break;
  126.         }
  127.         //
  128.         // Now, let's print the options
  129.         //
  130.         {
  131.             TOption*    opt = (TOption*)ret->opt.buf;
  132.             char        string[512];
  133.  
  134.             err = OTCreateOptionString("ip", &opt, ret->opt.buf + ret->opt.len,
  135.                                        string, sizeof(string));
  136.             
  137.             if ( err == kOTNoError )
  138.             {
  139.                 char*    str = string;
  140.                 size_t    len = 0;
  141.                 while ( true )
  142.                 {
  143.                     char* temp = strchr(str, ',');
  144.                     if ( temp == NULL )
  145.                     {
  146.                         fprintf(stderr, "%s\n", str);
  147.                         break;
  148.                     }
  149.                     if ( len + temp - str + 1 > 80 )
  150.                     {
  151.                         fprintf(stderr, "\n");
  152.                         if ( *str == ' ' )
  153.                             str += 1;
  154.                         len = 0;
  155.                     }
  156.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  157.                     len += temp - str + 1;
  158.                     str = temp + 1;
  159.                 }
  160.             }
  161.         }
  162.         
  163.         TOption* opt =  OTFindOption(ret->opt.buf, ret->opt.len, INET_IP, IP_TTL);
  164.             
  165.         if ( opt == NULL )
  166.             fprintf(stderr, "ERROR:OptionManagement did not have IP_TTL in returned options\n");
  167.  
  168.         //
  169.         // Get the current TCP options
  170.         //
  171.         fprintf(stderr, "Current Readable TCP Option Settings for endpoint @ %08lX:\n", ep);
  172.         option.len        = kOTOptionHeaderSize;
  173.         option.level    = INET_TCP;
  174.         option.name        = T_ALLOPT;
  175.         
  176.         req.opt.buf = (UInt8*)&option;
  177.         req.opt.len    = kOTOptionHeaderSize;
  178.         req.flags    = T_CURRENT;
  179.         
  180.         err = ep->OptionManagement(&req, ret);
  181.         if ( err != kOTNoError )
  182.         {
  183.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  184.             break;
  185.         }
  186.         //
  187.         // Now, let's print the options
  188.         //
  189.         {
  190.             TOption*    opt = (TOption*)ret->opt.buf;
  191.             char        string[512];
  192.  
  193.             err = OTCreateOptionString("tcp", &opt, ret->opt.buf + ret->opt.len,
  194.                                        string, sizeof(string));
  195.             
  196.             if ( err == kOTNoError )
  197.             {
  198.                 char*    str = string;
  199.                 size_t    len = 0;
  200.                 while ( true )
  201.                 {
  202.                     char* temp = strchr(str, ',');
  203.                     if ( temp == NULL )
  204.                     {
  205.                         fprintf(stderr, "%s\n", str);
  206.                         break;
  207.                     }
  208.                     if ( len + temp - str + 1 > 80 )
  209.                     {
  210.                         fprintf(stderr, "\n");
  211.                         if ( *str == ' ' )
  212.                             str += 1;
  213.                         len = 0;
  214.                     }
  215.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  216.                     len += temp - str + 1;
  217.                     str = temp + 1;
  218.                 }
  219.             }
  220.             fprintf(stderr, "\n");
  221.         }
  222.         
  223.     } while ( false );
  224.     OTFree(ret, T_OPTMGMT);
  225. }
  226.  
  227.  
  228. /*******************************************************************************
  229. ** Clean up at the end
  230. ********************************************************************************/
  231.  
  232. void CleanUp()
  233. {
  234.     CloseOpenTransport();
  235. }
  236.  
  237. /*******************************************************************************
  238. ** Idle
  239. ********************************************************************************/
  240.  
  241. void Idle()
  242. {
  243.     SystemTask();
  244. }
  245.  
  246. /*******************************************************************************
  247. ** EventHandler
  248. ********************************************************************************/
  249.  
  250. pascal void EventHandler(void*, OTEventCode event, OTResult, void*)
  251. {
  252.     OTEventCode tempevent = 0;
  253.  
  254.     switch ( event )
  255.     {
  256.         case T_BINDCOMPLETE:
  257.                             gBindCompleted = 1;
  258.                             break;
  259.         case T_ORDREL:
  260.                             gCallRcvOrdDiscon = 1;
  261.                             break;
  262.         default:
  263.                             DebugStr("\pEventHandler got unexpected event");
  264.                             break;
  265.     }
  266.     return;
  267. }
  268.  
  269. /*******************************************************************************
  270. ** DoIt
  271. ********************************************************************************/
  272.  
  273. void DoIt()
  274. {
  275.     TEndpoint*        ep = NULL;
  276.     TEndpointInfo    info;
  277.     TBind            req, ret;
  278.     TCall            sndcall, rcvcall;
  279.     OSStatus        err = kOTNoError;
  280.     long            myport = 0;
  281.     InetHost        myaddr = 0;
  282.     char            mystr[255];
  283.     long            bytes = 0;
  284.     OTFlags            flags = 0;
  285.  
  286.     myport = 0;
  287.     fprintf(stderr, "Pitch port ? (enter TCP port number)\n");
  288.     if ( gets(mystr) != 0 )
  289.     {
  290.         stringtonum(mystr, &myport);
  291.         gPitchPort =(InetPort)  myport;
  292.     }
  293.     myaddr = 0;
  294.     fprintf(stderr, "Pitch IP address ? (enter IP address)\n");
  295.     if ( gets(mystr) != 0 )
  296.     {
  297.         if (OTInetStringToHost(mystr, &myaddr) == 0)
  298.         {
  299.             gPitchIpAddr = (InetHost) myaddr;
  300.         }
  301.     }
  302.     myport = 0;
  303.     fprintf(stderr, "Catch port ? (enter port number)\n");
  304.     if ( gets(mystr) != 0 )
  305.     {
  306.         stringtonum(mystr, &myport);
  307.         gCatchPort = (InetPort) myport;
  308.     }
  309.     myaddr = 0;
  310.     fprintf(stderr, "Catch IP address ? (enter IP address)\n");
  311.     if ( gets(mystr) != 0 )
  312.     {
  313.         if ( OTInetStringToHost(mystr, &myaddr) == 0 ) 
  314.         {
  315.             gCatchIpAddr = (InetHost) myaddr;
  316.         }
  317.     }
  318.  
  319.     fprintf(stderr, "What should I send ? (enter data string)\n");
  320.     if ( gets(data) == 0 )
  321.     {
  322.         strcpy(data, defaultData);
  323.         fprintf(stderr, "sending default data: <%s>\n", data);
  324.     }
  325.     data[strlen(data)] = '\n';
  326.  
  327.     OTInetHostToString(gCatchIpAddr, mystr);
  328.     fprintf(stderr, "The program will send a packet to <%s:%d> on port <%d>\n", mystr, gCatchPort, gPitchPort);
  329.  
  330.     memset(&sndsin, 0, sizeof(struct InetAddress));
  331.     memset(&rcvsin, 0, sizeof(struct InetAddress));
  332.     memset(&sndcall, 0, sizeof(TCall));
  333.     memset(&rcvcall, 0, sizeof(TCall));
  334.     memset(&reqsin, 0, sizeof(struct InetAddress));
  335.     memset(&rcvsin, 0, sizeof(struct InetAddress));
  336.     memset(&req, 0, sizeof(TBind));
  337.     memset(&ret, 0, sizeof(TBind));
  338.  
  339.     OTInitInetAddress(&sndsin, gCatchPort, gCatchIpAddr);
  340.  
  341.     do
  342.     {
  343.         //
  344.         // Now create a TCP
  345.         //
  346.         ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &info, &err);
  347.  
  348.         if ( ep == NULL || err != kOTNoError )
  349.         {
  350.             ep = NULL;
  351.             fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
  352.             break;
  353.         }
  354.  
  355.         err = ep->SetSynchronous();
  356.         if ( err != kOTNoError )
  357.         {
  358.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  359.             break;
  360.         }
  361.  
  362.         //
  363.         // Install notifier we're going to use for testing
  364.         //
  365.         err = ep->InstallNotifier(&EventHandler, 0);
  366.         if ( err != kOTNoError )
  367.         {
  368.             fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
  369.             break;
  370.         }
  371.  
  372.         //
  373.         // Try to bind
  374.         // 
  375.         OTInitInetAddress(&reqsin, gPitchPort, gPitchIpAddr);
  376.  
  377.         req.addr.len = sizeof(struct InetAddress);
  378.         req.addr.buf = (unsigned char *) &reqsin;
  379.         req.qlen = 2;                                        // don't care for tcp
  380.         ret.addr.maxlen = sizeof(struct InetAddress);
  381.         ret.addr.buf = (unsigned char *) &retsin;
  382.  
  383.         // bind TCP to current address and port
  384.         err = ep->Bind(&req, &ret);
  385.         if ( err != kOTNoError )
  386.         {
  387.             fprintf(stderr, "ERROR: Bind() failed with %d\n", err);
  388.             break;
  389.         }
  390.         err = ep->SetSynchronous();
  391.         if ( err != kOTNoError )
  392.         {
  393.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  394.             break;
  395.         }
  396.  
  397.         ShowEndpointOptions(ep);
  398.  
  399.         sndcall.addr.len = sizeof(struct InetAddress);
  400.         sndcall.addr.buf = (unsigned char *) &sndsin;
  401.     
  402.         rcvcall.addr.maxlen = sizeof(struct InetAddress);
  403.         rcvcall.addr.buf = (unsigned char *) &rcvsin;
  404.  
  405.         err = ep->Connect(&sndcall, &rcvcall);
  406.         if ( err != kOTNoError )
  407.         {
  408.             fprintf(stderr, "ERROR: Connect() failed with %d\n", err);
  409.             break;
  410.         }
  411.  
  412.         bytes = ep->Snd(data, strlen(data), flags);
  413.         if ( bytes >= 0 )
  414.         {
  415.             fprintf(stderr, "Sent packet\nbytes: <%d> data: %s", bytes, data);
  416.         }
  417.         else 
  418.         {
  419.             fprintf(stderr, "ERROR: Snd() failed with %d\n", bytes);
  420.         }
  421.         Idle();
  422.     } while (false);
  423.  
  424.     if ( ep != NULL )
  425.     {
  426.         err = ep->SndOrderlyDisconnect();
  427.         if ( err != kOTNoError )
  428.         {
  429.             if ( err == kOTLookErr )
  430.                 fprintf(stderr, "SndOrderlyDisconnect() returns %d\n", err);
  431.             else
  432.                 fprintf(stderr, "ERROR: SndOrderlyDisconnect() failed with %d\n", err);
  433.         }
  434.     
  435.         while (gCallRcvOrdDiscon == 0)
  436.             Idle();
  437.         err = ep->RcvOrderlyDisconnect();
  438.         if ( err != kOTNoError )
  439.         {
  440.             fprintf(stderr, "ERROR: RcvOrderlyDisconnect() failed with %d\n", err);
  441.         }
  442.     
  443.         //
  444.         // Remove notifier
  445.         //
  446.         ep->RemoveNotifier();    
  447.         //
  448.         // Try to Unbind
  449.         // 
  450.     /*
  451.         err = ep->Unbind();
  452.         if ( err != kOTNoError )
  453.         {
  454.             fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
  455.         }
  456.     */
  457.         //
  458.         // Get rid of endpoint.
  459.         //
  460.         err = OTCloseProvider(ep);
  461.         if ( err != kOTNoError )
  462.         {
  463.             fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
  464.         }
  465.     }
  466.     fprintf(stderr, "Bye\n");
  467. }
  468.